{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "确定交易的价格区间，区间包含最高价，输入总资金，建仓价，建仓金额或者股数，回本浮亏幅度，计算最佳加仓次数、间隔、加仓金额"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "   下跌金额  加仓价格 下跌幅度  加仓次数  建仓价格 回建仓价所需涨幅    加仓金额    总加仓金额  加仓手数  总手数  最新成本价  \\\n0  0.00  6.70   0%     0   6.7      0.0  9380.0   9380.0    14   14   6.70   \n1  0.67  6.03  10%     1   6.7    11.11  9648.0  19028.0    16   30   6.34   \n2  1.34  5.36  20%     2   6.7     25.0  9648.0  28676.0    18   48   5.97   \n3  2.01  4.69  30%     3   6.7    42.86  9849.0  38525.0    21   69   5.58   \n4  2.68  4.02  40%     4   6.7    66.67  9648.0  48173.0    24   93   5.18   \n5  3.35  3.35  50%     5   6.7    100.0  9715.0  57888.0    29  122   4.74   \n6  4.02  2.68  60%     6   6.7    150.0  9916.0  67804.0    37  159   4.26   \n7  4.69  2.01  70%     7   6.7   233.33  9849.0  77653.0    49  208   3.73   \n8  5.36  1.34  80%     8   6.7    400.0  9916.0  87569.0    74  282   3.11   \n9  6.03  0.67  90%     9   6.7    900.0  9983.0  97552.0   149  431   2.26   \n\n      回本空间  \n0     0.0%  \n1    5.14%  \n2   11.38%  \n3   18.98%  \n4   28.86%  \n5   41.49%  \n6   58.96%  \n7   85.57%  \n8  132.09%  \n9  237.31%  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>下跌金额</th>\n      <th>加仓价格</th>\n      <th>下跌幅度</th>\n      <th>加仓次数</th>\n      <th>建仓价格</th>\n      <th>回建仓价所需涨幅</th>\n      <th>加仓金额</th>\n      <th>总加仓金额</th>\n      <th>加仓手数</th>\n      <th>总手数</th>\n      <th>最新成本价</th>\n      <th>回本空间</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.00</td>\n      <td>6.70</td>\n      <td>0%</td>\n      <td>0</td>\n      <td>6.7</td>\n      <td>0.0</td>\n      <td>9380.0</td>\n      <td>9380.0</td>\n      <td>14</td>\n      <td>14</td>\n      <td>6.70</td>\n      <td>0.0%</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.67</td>\n      <td>6.03</td>\n      <td>10%</td>\n      <td>1</td>\n      <td>6.7</td>\n      <td>11.11</td>\n      <td>9648.0</td>\n      <td>19028.0</td>\n      <td>16</td>\n      <td>30</td>\n      <td>6.34</td>\n      <td>5.14%</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1.34</td>\n      <td>5.36</td>\n      <td>20%</td>\n      <td>2</td>\n      <td>6.7</td>\n      <td>25.0</td>\n      <td>9648.0</td>\n      <td>28676.0</td>\n      <td>18</td>\n      <td>48</td>\n      <td>5.97</td>\n      <td>11.38%</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2.01</td>\n      <td>4.69</td>\n      <td>30%</td>\n      <td>3</td>\n      <td>6.7</td>\n      <td>42.86</td>\n      <td>9849.0</td>\n      <td>38525.0</td>\n      <td>21</td>\n      <td>69</td>\n      <td>5.58</td>\n      <td>18.98%</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2.68</td>\n      <td>4.02</td>\n      <td>40%</td>\n      <td>4</td>\n      <td>6.7</td>\n      <td>66.67</td>\n      <td>9648.0</td>\n      <td>48173.0</td>\n      <td>24</td>\n      <td>93</td>\n      <td>5.18</td>\n      <td>28.86%</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>3.35</td>\n      <td>3.35</td>\n      <td>50%</td>\n      <td>5</td>\n      <td>6.7</td>\n      <td>100.0</td>\n      <td>9715.0</td>\n      <td>57888.0</td>\n      <td>29</td>\n      <td>122</td>\n      <td>4.74</td>\n      <td>41.49%</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>4.02</td>\n      <td>2.68</td>\n      <td>60%</td>\n      <td>6</td>\n      <td>6.7</td>\n      <td>150.0</td>\n      <td>9916.0</td>\n      <td>67804.0</td>\n      <td>37</td>\n      <td>159</td>\n      <td>4.26</td>\n      <td>58.96%</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>4.69</td>\n      <td>2.01</td>\n      <td>70%</td>\n      <td>7</td>\n      <td>6.7</td>\n      <td>233.33</td>\n      <td>9849.0</td>\n      <td>77653.0</td>\n      <td>49</td>\n      <td>208</td>\n      <td>3.73</td>\n      <td>85.57%</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>5.36</td>\n      <td>1.34</td>\n      <td>80%</td>\n      <td>8</td>\n      <td>6.7</td>\n      <td>400.0</td>\n      <td>9916.0</td>\n      <td>87569.0</td>\n      <td>74</td>\n      <td>282</td>\n      <td>3.11</td>\n      <td>132.09%</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>6.03</td>\n      <td>0.67</td>\n      <td>90%</td>\n      <td>9</td>\n      <td>6.7</td>\n      <td>900.0</td>\n      <td>9983.0</td>\n      <td>97552.0</td>\n      <td>149</td>\n      <td>431</td>\n      <td>2.26</td>\n      <td>237.31%</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "'''\n",
    "输入项\n",
    "'''\n",
    "total_amount = 100000\n",
    "position_build_price = 6.7\n",
    "position_build_amount = 10000\n",
    "back_rate = 10\n",
    "\n",
    "# 跌幅初始值\n",
    "default_down_rate = 0\n",
    "'''\n",
    "跌幅步进\n",
    "'''\n",
    "down_step = 10\n",
    "\n",
    "'''\n",
    "加仓倍数\n",
    "'''\n",
    "add_mutiply = 1\n",
    "\n",
    "'''\n",
    "加仓手数或者资金步进\n",
    "'''\n",
    "\n",
    "data = pd.DataFrame(columns = [\"下跌金额\",\"加仓价格\",\"下跌幅度\",\"加仓次数\",\"建仓价格\",\"回建仓价所需涨幅\",\"加仓金额\",\"总加仓金额\",\"加仓手数\",\"总手数\",\"最新成本价\",\"回本空间\"])\n",
    "add_times = -1\n",
    "last_amount = position_build_amount\n",
    "total_lots = 0\n",
    "total_add_amount = 0\n",
    "for i in range(default_down_rate,100,down_step):\n",
    "    if last_amount < position_build_amount:\n",
    "        last_amount = position_build_amount\n",
    "    down_fix = round(position_build_price*(i/100),2)\n",
    "    add_price = round(position_build_price-down_fix,2)\n",
    "    add_times+=1\n",
    "    back_to_position_build_price_rate = round((position_build_price-add_price)/add_price*100,2)\n",
    "    add_lots = int((last_amount/add_price)/100)\n",
    "    last_amount = round(add_price*add_lots*100,2)\n",
    "\n",
    "    total_lots += add_lots\n",
    "    total_add_amount += last_amount\n",
    "    cost = round(total_add_amount/(total_lots*100),2)\n",
    "    back_to_cost_rate = round((cost-add_price)/add_price*100,2)\n",
    "\n",
    "    row=[down_fix,add_price,str(i)+\"%\",add_times,position_build_price,str(back_to_position_build_price_rate),last_amount,total_add_amount,add_lots,total_lots,cost,str(back_to_cost_rate)+\"%\"]\n",
    "    data.loc[len(data)] = row\n",
    "    last_amount=last_amount*add_mutiply\n",
    "data\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
